package com.dlwx.parttimetalent.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.dlwx.parttimetalent.R;
import com.dlwx.parttimetalent.base.BaseActivity;
import com.dlwx.parttimetalent.bean.NoSetIntentBean;
import com.dlwx.parttimetalent.comm.Constant;
import com.dlwx.parttimetalent.comm.Http;
import com.dlwx.parttimetalent.utils.DealResult;
import com.dlwx.parttimetalent.utils.OkgoUtil;
import com.dlwx.parttimetalent.utils.ScreenUtils;
import com.dlwx.parttimetalent.utils.SizeUtils;
import com.dlwx.parttimetalent.utils.ToastUtils;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class HyyrThressActivity extends BaseActivity {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.flowlayout)
    TagFlowLayout flowlayout;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;
    private int itemWidth;
    private NoSetIntentBean.BodyBean ChooseBean;
    private int labelclassid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hyyr_thress);
        ButterKnife.bind(this);
        int width= ScreenUtils.getScreenWidth();
        itemWidth=(int) (width - SizeUtils.dp2px(40f)) / 5;;
        labelclassid=getIntent().getIntExtra("id",0);
        initView();
        getDatas();
    }

    private void getDatas() {
        loadingDialog.show();
        OkgoUtil.post(Http.IndustrySon, HyyrThressActivity.this)
                .params("labelclassid",labelclassid)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        loadingDialog.dismiss();
                        NoSetIntentBean parentBean = DealResult.getInstance().dealDatas(HyyrThressActivity.this, response, new TypeToken<NoSetIntentBean>() {
                        }.getType());
                        if (parentBean == null) {
                            return;
                        }
                        if (parentBean.getCode() == Constant.SUCESS||parentBean.getCode()==Constant.EMPTY) {
                            setDatas(parentBean.getBody());
                        }else {
                            ToastUtils.toastForShort(HyyrThressActivity.this, parentBean.getResult());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDialog.dismiss();
                        ToastUtils.toastForShort(HyyrThressActivity.this, getString(R.string.bad_net));
                    }
                });
    }

    private void setDatas(final List<NoSetIntentBean.BodyBean> body) {
        flowlayout.setMaxSelectCount(1);
        TagAdapter<NoSetIntentBean.BodyBean> tagAdapter=new TagAdapter<NoSetIntentBean.BodyBean>(body) {
            @Override
            public View getView(FlowLayout parent, int position, NoSetIntentBean.BodyBean labelBean) {
                String tip = labelBean.getLabelname();
                TextView textView = (TextView) LayoutInflater.from(HyyrThressActivity.this)
                        .inflate(R.layout.tv_label_red_select, flowlayout, false);
                textView.setText(tip);
                textView.getLayoutParams().width = itemWidth;
                return textView;
            }
        };
        flowlayout.setAdapter(tagAdapter);
        flowlayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                ChooseBean=body.get(position);
                return false;
            }
        });
    }

    private void initView( ) {
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        tvMiddleTitle.setVisibility(View.VISIBLE);
        tvMiddleTitle.setText("行业用人");

    }

    @OnClick({R.id.title_left_one_btn, R.id.tv_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                this.finish();
                break;
            case R.id.tv_confirm:
                if(ChooseBean==null){
                    ToastUtils.toastForShort(this,"请选择一个类目");
                    return;
                }
                Intent i=new Intent(this,EditLabelDetailActivity.class);
                i.putExtra("name",ChooseBean.getLabelname());
                i.putExtra("classId",labelclassid);
                i.putExtra("labelId",ChooseBean.getLabelid()+"");
                i.putExtra("flag",Constant.ONE);
                startActivity(i);
                this.finish();
                break;
        }
    }
}
